package com.niit.springbootshop.controller;

import com.niit.springbootshop.service.CategoryService;
import com.niit.springbootshop.service.ProductService;
import com.niit.springbootshop.tools.ImageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @program: springboot-demo
 * @description:
 * @author: hanliang
 * @create: 2021-05-07 15:10
 **/
@Controller
public class IndexController {

    @Autowired
    ProductService productService;

    @Autowired
    CategoryService categoryService;

    /**
     * 跳转到首页
     * @param map
     * @return
     */
    @RequestMapping("/")
    public String index(ModelMap map, HttpSession session){// ModelMap往界面传数据

        map.addAttribute("hotProducts",productService.getHotProducts());
        // 一级分类查询出来并存入session
        session.setAttribute("MENU_LIST",categoryService.getAll());

        return "index"; // 跳转到index.html
    }

    @RequestMapping("/getCode")
    public void index(HttpServletRequest request, HttpServletResponse response){// ModelMap往界面传数据
        ImageUtil util = new ImageUtil();
        util.drawCode(request,response);
    }
}
